package club325.webchat.database.hibernate.implement;

import java.util.List;

import org.hibernate.Query;
import club325.webchat.database.hibernate.dao.FriendsDAO;
import club325.webchat.database.hibernate.dao.HibernateUtil;
import club325.webchat.database.hibernate.domain.Friends;

public class FriendsImplement implements FriendsDAO
{

	public void saveFriends(Friends friend)
	{
		HibernateUtil.add(friend);
	}

	public Friends findFriendsByID(int id)
	{
		return (Friends)HibernateUtil.get(Friends.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Friends> findFriendsByOwner(String Owner)
	{
		String HQL = "FROM Friends AS friends WHERE friends.owner = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0, Owner);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public List<Friends> findFriendsByFriendList(String ListName)
	{
		String HQL = "FROM Friends AS friends WHERE friends.friendlist = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0, ListName);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public List<Friends> findFriendsByFriendID(String FriendID)
	{
		String HQL = "FROM Friends AS friends WHERE friends.friendID = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0, FriendID);
		return q.list();
	}

//	public void UpdateFriends(Friends friend)
//	{
//		HibernateUtil.Update(friend);
//	}

	public void remove(Friends friend)
	{
		HibernateUtil.delete(friend);
	}

	@SuppressWarnings("unchecked")
	public List<String> getAllfriends(String OwnerID)
	{
		String HQL = "FROM Friends AS friends WHERE friends.owner = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0,OwnerID);
		return q.list();
	}
}
